from django.http import JsonResponse
from calc.models import RecyclingInfo
from system.models import Department, Manager
from common.page_util import page_util
from django.db import connection
import json


def home_data(request):
    response = {}
    area_list = []
    hefei = {}
    chaohu = {}
    lujiang = {}
    feixi = {}
    feidong = {}
    changfeng = {}
    cursor = connection.cursor()
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 1')
    hefei['value'] = cursor.fetchone()[0]
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 2')
    chaohu['value'] = cursor.fetchone()[0]
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 3')
    lujiang['value'] = cursor.fetchone()[0]
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 4')
    changfeng['value'] = cursor.fetchone()[0]
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 5')
    feixi['value'] = cursor.fetchone()[0]
    cursor.execute(
        'SELECT count(*) from(select r.recycling_department,d.area from calc_recyclinginfo r,system_department d WHERE r.recycling_department = d.department_name) t where t.area = 6')
    feidong['value'] = cursor.fetchone()[0]
    hefei['name'] = "合肥市区"
    chaohu['name'] = "巢湖市"
    lujiang['name'] = "庐江县"
    changfeng['name'] = "长丰县"
    feixi['name'] = "肥西县"
    feidong['name'] = "肥东县"
    area_list.append(hefei)
    area_list.append(chaohu)
    area_list.append(changfeng)
    area_list.append(lujiang)
    area_list.append(feixi)
    area_list.append(feidong)
    response['area_list'] = area_list
    # 设备类型统计
    e8dian_count = RecyclingInfo.objects.all().filter(equipment='E8-C电口设备').count()
    e8guang_count = RecyclingInfo.objects.all().filter(equipment='E8-C光口设备').count()
    biaoqing_count = RecyclingInfo.objects.all().filter(equipment='标清机顶盒').count()
    gaoqing_count = RecyclingInfo.objects.all().filter(equipment='高清机顶盒').count()
    wuxianmao_count = RecyclingInfo.objects.all().filter(equipment='无线猫').count()
    device_name_list = ['E8-C电口设备', 'E8-C光口设备', '标清机顶盒', '高清机顶盒', '无线猫']
    device_count_list = [e8dian_count, e8guang_count, biaoqing_count, gaoqing_count,  wuxianmao_count]

    # 用户总数统计
    user_count = Manager.objects.all().count()

    # 部门总数统计
    depart_count = Department.objects.all().count()

    # 回收单总数统计
    recycle_count = RecyclingInfo.objects.all().count()

    response['device_name_list'] = device_name_list
    response['device_count_list'] = device_count_list
    response['user_count'] = user_count
    response['depart_count'] = depart_count
    response['recycle_count'] = recycle_count
    response['status'] = 200
    response['msg'] = '确认成功'
    return JsonResponse(response)


def home_message(request):
    response = {}
    data = json.loads(request.body)
    department_id = data['department']
    if department_id == '0':
        count = RecyclingInfo.objects.all().filter(state='0').count()
    else:
        count = RecyclingInfo.objects.all().filter(recycling_department_id=department_id, state='0').count()
    response['message'] = count
    return JsonResponse(response)